module WindSpirit
{
    export class LoadingLayer extends egret.DisplayObjectContainer
    {
        public completeFunc;
        public gardenComplete = false;
        public loadingMc: LoadingMC;
        public timer: egret.Timer;
        public currentTime = 0;
        public aniComplete = false;
        public EVENT_ALL_COMPLETE = "EVENT_ALL_COMPLETE";
        public labelPersent: egret.TextField;
        public light: LightLabel;
        public realPersent = 0;
        public constructor(thisObj,completeFunc: Function)
        {
            super();
            WindSpirit.LoadResource.init();
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoadComplete,this);

            var bg = new egret.Shape();
            bg.graphics.beginFill(0x232A32);
            bg.graphics.drawRect(0,0,WindSpirit.Util.stageW,WindSpirit.Util.stageH);
            //this.addChild(bg);
            //var garden = new Garden();
            //this.addChild(garden);
            var light = new LightLabel("风精灵新媒体");
            light.x = WindSpirit.Util.centerX;
            light.y = WindSpirit.Util.centerY;
            this.light = light;
            this.addChild(light);

            var loadingMc = new WindSpirit.LoadingMC;
            loadingMc.y = light.y + 30 + ((light.height + loadingMc.height) >> 1);
            loadingMc.x = light.x;
            this.loadingMc = loadingMc;
            this.loadingMc.addEventListener(egret.Event.COMPLETE,() =>
            {
                console.log("loading mc end");
                this.aniComplete = true;
                this.dispatchEventWith(egret.Event.COMPLETE);
            },this);
            this.addChild(loadingMc);

            this.labelPersent = new egret.TextField;
            this.labelPersent.anchorX = this.labelPersent.anchorY = 0.5;
            this.labelPersent.size = 0.083 * WindSpirit.Util.stageW;
            this.labelPersent.x = light.x;
            this.labelPersent.y = this.loadingMc.y + this.loadingMc.height / 2 + this.labelPersent.height / 2 + 50;
            this.addChild(this.labelPersent);

            var timer = new egret.Timer(30);
            timer.addEventListener(egret.TimerEvent.TIMER,this.time,this);
            timer.start();
            this.timer = timer;
            this.addEventListener(egret.Event.COMPLETE,this.onComplete,this);
            if(completeFunc)
            {
                this.completeFunc = completeFunc;
                this.addEventListener(this.EVENT_ALL_COMPLETE,this.completeFunc,thisObj);
            }
        }
        public onResourceLoadComplete(event: RES.ResourceEvent): void
        {
            if(event.groupName == WindSpirit.Const.WIND_RESGROUP_NAME)
            {
                console.log("windspirit res complete");
                RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoadComplete,this);
                LoadResource.isResourceLoaded = true;
                this.dispatchEventWith(egret.Event.COMPLETE);
            }
        }
        public time()
        {
            var persent = this.currentTime / 100;
            if(this.realPersent >= persent)//资源加载过快以定时器为准
            {
                this.setProgress(persent);
                this.currentTime++;
                if(this.currentTime > 100)
                {
                    this.light.stop();
                    this.timer.stop();
                }
            }
        }
        public setPersent(persent)
        {
            this.realPersent = persent;
            var timePersent = this.currentTime / 100;
            if(this.realPersent < timePersent)
            {
                this.setProgress(this.realPersent);
            }
        }
        public setProgress(persent)
        {
            this.labelPersent.text = "加载游戏中..." + this.currentTime + "%";
            this.loadingMc.setPregress(persent);
        }
        public onComplete()
        {
            if(this.aniComplete && LoadResource.isResourceLoaded)
            {
                this.parent.removeChild(this);
                this.dispatchEventWith(this.EVENT_ALL_COMPLETE);
            }
        }
    }
}